<template>
  <Card style="width:850px;margin: auto;">
    <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="150"
          style="width: 300px;margin: auto;">

      <FormItem label="半成品类型代码:" prop="code">
        <Input v-model="formLeft.code" :maxlength="2" disabled></Input>
      </FormItem>

      <FormItem label="半成品类型名称:" prop="name">
        <Input v-model="formLeft.name" :maxlength="10"></Input>
      </FormItem>


      <FormItem label="描述:">
        <Input v-model="formLeft.description" type="textarea"></Input>
      </FormItem>


      <FormItem label="使用状态" prop="state">
        <Select v-model="formLeft.state">
          <Option v-for="item in stateList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </FormItem>

      <row type="flex" justify="center">
        <i-col span="20">

        </i-col>
        <i-col span="30">
          <Button type="primary" @click="SemiTypeUpdateByCode">修改</Button>
          <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
        </i-col>
      </row>

    </Form>
  </Card>
</template>

<script>
  import {SemiTypeUpdateByCode, SemiTypeFindByCode} from "../../../api/data"

  export default {
    data() {
      return {
        page: 1,//page 第几页
        pageSize: 10,//pageSize:每页几条数据
        total: 0,
        stateList: [
          {
            value: '0',
            label: '不使用'
          }, {
            value: '1',
            label: '使用'
          }
        ],
        formLeft: {
          code: '',
          name: '',
          state: '',
          description: ''
        },
        validateLibForm: {
          code: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          state: [
            {required: true, message: '请选择', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '请选择', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {
      const codeFlag = this.$route.query.code;
      if (codeFlag) {
        this.formLeft.code = codeFlag;
        this.SemiTypeFindByCode();
      }
    },
    methods: {
      SemiTypeUpdateByCode() {
        if (this.formLeft.code !== null && this.formLeft.code !== "") {
          if (this.formLeft.code.length == 2) {
            if (this.formLeft.name !== null && this.formLeft.name !== "") {
              if (this.formLeft.state !== null && this.formLeft.state !== "") {
                SemiTypeUpdateByCode(this.formLeft).then(res => {
                  if (res.data.code == 0) {
                    this.$Message.success("编辑成功");
                    this.$router.go(-1);
                  } else {
                    this.$Message.error(res.data.msg);
                  }
                })
              } else {
                this.$Message.error("使用状态不能为空");
              }
            } else {
              this.$Message.error("半成品类型不能为空");
            }
          } else {
            this.$Message.error("半成品类型格式不满足需求");
          }
        } else {
          this.$Message.error("半成品类型不能为空");
        }
      },
      back() {
        this.$router.go(-1);
      },
      SemiTypeFindByCode() {
        var data = {
          code: this.formLeft.code,
        };
        SemiTypeFindByCode(data).then(res => {
          this.formLeft = res.data.data;
        })
      }
    },

  }
</script>


